use([
    'public/javascripts/service/resetChangePwService.js',
    'public/javascripts/widget/step/step.js',
    'public/javascripts/widget/prompt/prompt.js',
    'public/common/des.js'
    ],
    function(resetChangePwService, StepComponent, PromptComponent, Des) {
    var PageComponent = React.createClass({
        eventObj: {},
        countdown: 60,
        componentDidMount: function() {
            $('#tips').hover(
                function() {
                    $('#tipsMsg').css('display', 'block');
                },
                function() {
                    $('#tipsMsg').css('display', 'none');
                }
            );
            $('#phoneNumber').attr('maxlength', '11');
            $('#newPassword').attr('maxlength', '18');
            $('#repeateNewPassword').attr('maxlength', '18');
        },
        receiveMsg: function(data) {},
        getInitialState: function() {
            return {
                current: 1,
                newPassword: '',
                repeateNewPassword: '',
                passWordLevel: 'original',
                ExistNoSupWord: false,
                ExistPwNotSame: false,
                ExistPwLow: false
            };
        },
        ChangeValue: function(event) {
            var name = event.target.name;
            var value = event.target.value;
            this.eventObj[name] = value;
            this.setState(this.eventObj);
        },
        confirmModify: function() {
            var that = this;

            if (!this.state.newPassword || !this.state.repeateNewPassword) {
                return;
            }

            this.CheckNewPwRule();
            this.CheckPwTwiceSame();
            if (this.state.ExistPwLow || this.state.ExistPwNotSame) {
                return;
            }

            var param = {
                newPassword: Des.enc(this.state.newPassword),
                repeateNewPassword: Des.enc(this.state.repeateNewPassword)
            };
            resetChangePwService.resetChangePw({ urlParams: param }, function(returnData) {
                if (returnData.errorCode == 0) {
                    that.setState({ current: 2 });
                } else {
                    PromptComponent.init({ type: 'warning', msg: returnData.errorMsg}).show();
                }
            });
        },
        initPasswordStrength: function() {
            var O_color = '#eeeeee';
            document.getElementById('strength_L').style.background = O_color;
            document.getElementById('strength_M').style.background = O_color;
            document.getElementById('strength_H').style.background = O_color;
            this.setState({ passWordLevel: 'original' });
        },
        CalcLevel: function(mode) {
            var level = 0;
            for (var indexj = 0; indexj < 4; indexj++) {
                if (mode & 1) {
                    level++;
                }
                mode >>>= 1;
            }
            return level;
        },
        checkPwFormat: function(event) {
            var password = this.state.newPassword;

            var O_color = '#eeeeee';
            var L_color = '#f6cd8a';
            var M_color = '#fab749 ';
            var H_color = '#f68c23';
            var level = 0;

            var mode = 0;
            var Lcolor;
            var Mcolor;
            var Hcolor;

            this.setState({ ExistNoSupWord: false });
            this.setState({ ExistPwLow: false });

            if (password == null || password == '' || password == undefined) {
                Lcolor = Mcolor = Hcolor = O_color;
                mode = 0;
            } else {
                for (var index = 0; index < password.length; index++) {
                    var charMode;
                    var regSpecial = /((?=[\x21-\x7e]+)[^A-Za-z0-9])/;
                    var regNumber = /[0-9]+/;
                    var regLowercase = /[a-z]+/;
                    var regCapital = /[A-Z]+/;

                    var singleWord = password.charAt(index);

                    // 判断输入密码的类型
                    if (regNumber.test(singleWord)) {
                        //数字
                        charMode = 1;
                    } else if (regCapital.test(singleWord))
                        //大写
                        charMode = 2;
                    else if (regLowercase.test(singleWord))
                        //小写
                        charMode = 4;
                    else if (regSpecial.test(singleWord)) {
                        charMode = 8; //特殊字符
                    } else {
                        //不支持的字符
                        this.setState({ ExistNoSupWord: true });
                        this.initPasswordStrength();
                        return;
                    }

                    mode |= charMode;

                    var typeNumber = this.CalcLevel(mode);
                }


                if (typeNumber == 0 || password.length > 18) {
                    Lcolor = Mcolor = Hcolor = O_color;
                    this.setState({ passWordLevel: 'original' });
                } else if (password.length < 6 || typeNumber <= 2) {
                    Lcolor = L_color;
                    Mcolor = Hcolor = O_color; ///低优先级
                    this.setState({ passWordLevel: 'low' });
                } else if ((password.length >= 6 && password.length <= 9 && typeNumber == 3) || (password.length >= 6 && password.length <= 7 && typeNumber == 4)) {
                    Lcolor = Hcolor = O_color;
                    Mcolor = M_color; ///中级
                    this.setState({ passWordLevel: 'normal' });
                } else if ((password.length >= 10 && password.length <= 18 && typeNumber == 3) || (password.length >= 8 && password.length <= 18 && typeNumber == 4)) {
                    Hcolor = H_color; ///高级
                    Lcolor = Mcolor = O_color;
                    this.setState({ passWordLevel: 'high' });
                } else {
                    Lcolor = Mcolor = Hcolor = O_color;
                    this.setState({ passWordLevel: 'original' });
                }
            }
            document.getElementById('strength_L').style.background = Lcolor;
            document.getElementById('strength_M').style.background = Mcolor;
            document.getElementById('strength_H').style.background = Hcolor;
        },
        CheckNewPwRule: function() {
            if (this.state.passWordLevel == 'original' || this.state.passWordLevel == 'low') {
                this.setState({ ExistPwLow: true });
            } else {
                this.setState({ ExistPwLow: false });
            }
        },
        CheckPwTwiceSame: function() {
            if (this.state.newPassword != this.state.repeateNewPassword) {
                this.setState({ ExistPwNotSame: true });
            } else {
                this.setState({ ExistPwNotSame: false });
            }
        },
        showFormatErr: function() {
            var styleObj = {};

            if (this.state.ExistNoSupWord == true) {
                styleObj = { display: 'block' };
            } else {
                styleObj = { display: 'none' };
            }

            return styleObj;
        },
        showUnsamePwErr: function() {
            var styleObj = {};

            if (this.state.ExistPwNotSame) {
                styleObj = { display: 'block' };
            } else {
                styleObj = { display: 'none' };
            }

            return styleObj;
        },
        showPwTooLowErr: function() {
            var styleObj = {};

            if (!this.state.ExistNoSupWord && this.state.ExistPwLow) {
                styleObj = { display: 'block' };
            } else {
                styleObj = { display: 'none' };
            }

            return styleObj;
        },
        backTologin: function() {
            window.location.href = 'http://' + location.host + '/login';
        },
        refuseSumit: function(e) {
            e.preventDefault();
            return false;
        },
        render: function() {
            var data = this.state.data;
            return ~init;
        }
    });

    if (typeof ReactDOM != 'undefined') {
        ReactDOM.render(<PageComponent data={storage} />, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
